package com.rjt.user.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.rjt.common.util.StaticMethod;
import com.rjt.user.model.UserModel;
import com.rjt.user.repository.UserRepository;
import com.rjt.user.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	 
	@Autowired
	private HttpSession session;
	
	@Autowired
	private UserRepository userRepository;
	@Override
	public String register(UserModel userModel) {
		// TODO Auto-generated method stub
		String flag="success";
		try {
			userModel.setType(1);
			userModel.setCreateDate(new Date());
			userRepository.save(userModel);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("注册失败");
			flag="error";
		}
		return flag;
	}

	@Override
	public String login(String email, String password,HttpServletRequest request) {
		// TODO Auto-generated method stub
		UserModel user = new UserModel();
		user.setEmail(email);
		user.setPassword(password);
		 Example<UserModel> example = Example.of(user);
		 List<UserModel> users = userRepository.findAll(example);
		 if(users.size()==1) {
			 request.getSession().setAttribute("user", users.get(0));
			return "success" ;
		 }else {
			 return "error" ; 
		 }
	}

	@Override
	public Page<UserModel> getList(HttpServletRequest request) {
		// TODO Auto-generated method stub
		int curPage = StaticMethod.nullObject2int(request.getParameter("page")) <= 0 ? 1 : 
			StaticMethod.nullObject2int(request.getParameter("page"));
		int pageSize = StaticMethod.nullObject2int(request.getParameter("limit")) <= 0 ? 10 : 
			StaticMethod.nullObject2int(request.getParameter("limit"));
		 PageRequest pageRequest = new PageRequest(curPage-1, pageSize,Direction.DESC, "createDate");
		 return userRepository.findAll(pageRequest);
	}

	@Override
	public String delete(List<UserModel> user) {
		// TODO Auto-generated method stub
		String flag="success";
		 try {
			 userRepository.delete(user);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("用户删除失败");
			flag="error";
		}
		 return flag;
	}

	@Override
	public UserModel findUser(Integer id) {
		// TODO Auto-generated method stub
		return userRepository.findOne(id);
	}

	@Override
	public String add(UserModel user, String method) {
		// TODO Auto-generated method stub
		String flag="success";
		try {
			user.setCreateDate(new Date());
			userRepository.save(user);
		} catch (Exception e) {
			// TODO: handle exception
			flag="error";
		}
		return flag;
	}


	@Override
	public boolean isAdmin() {
		UserModel userSession=(UserModel) session.getAttribute("user");
		if(userSession!=null && userSession.getType()>=2) {
			return true;
		}
		return false;
	}
	
	
	@Override
	public boolean isLogin() {
		UserModel userSession=(UserModel) session.getAttribute("user");
		if(userSession!=null) {
			return true;
		}
		return false;
	}
	
	@Override
	public String getuId() {
		// TODO Auto-generated method stub
		UserModel userSession=(UserModel) session.getAttribute("user");
		if(userSession!=null) {
			return userSession.getId().toString();
		}
		return "";
	}

	@Override
	public String checkUser(UserModel userModel) {
		// TODO Auto-generated method stub
		 UserModel user = new UserModel();
		 user.setEmail(userModel.getEmail());
		 Example<UserModel> example = Example.of(user);
		 List<UserModel> users = userRepository.findAll(example);
		 if(users.size()>=1) {
			return "error" ;
		 }else {
			 return "success" ; 
		 }
	}
	
	
}
